
#include "Worker.h"

extern Counter g_SelectCounter;
extern Counter g_UpdateCounter;
extern Counter g_UpRoleIDCounter;

// 异步处理函数
void  Async_SelectPlayerName(CMsgTyped* pmsg,  sql::Statement* pConn)
{
    if (!pmsg || !pConn) return;

    int index = pmsg->IntVal();
    UI32 max_id = 0;
   // printf("Async_SelectPlayerName %d\n", index);

    const char* sql = "select name from GlobalRoles";
    vector<string> * names = new  vector<string>;

    MysqlResult res;
	try
	{
		res.dataSet = pConn->executeQuery(sql);

		if (res.dataSet)
		{
            while (res.dataSet->next())
            {
                string player = res.dataSet->getString("name");
                names->push_back( player );
            }
		}
	}
	catch(sql::SQLException & e)
    {
        cout << __LINE__ << "\n";
        cout << "# ERR: " << e.what() << "\n";
    }

    //////////////////////////////
    MysqlResult res2;
	try
	{
		res2.dataSet = pConn->executeQuery("select max(roleId) as maxId from GlobalRoles");

		if (res2.dataSet && res2.dataSet->next() )
		{
            max_id = res2.dataSet->getInt("maxId");
		}
	}
	catch(sql::SQLException & e)
    {
        cout << __LINE__ << "\n";
        cout << "# ERR: " << e.what() << "\n";
    }

    ///////////////////////////////

    // 拷贝数据
    _WORKER->Lock();

    for (size_t i=0;  i < names->size(); i++)
    {
        _WORKER->AddPlayerName(  (*names)[i].c_str(), index );
    }

    if (index <= 0)
    {
        _CONFIG->master.role_id_max = max_id;
    }
    else
    {
        _CONFIG->slaves[ index - 1].role_id_max = max_id;
    }

    _WORKER->Unlock();

    delete names;
    g_SelectCounter.DecRef();
}

void  Async_SelectFamilyName(CMsgTyped* pmsg,  sql::Statement* pConn)
{
    if (!pmsg || !pConn) return;

    int index = pmsg->IntVal();
    //printf("Async_SelectFamilyName %d\n", index);

    const char* sql = "select FamilyName from FamilyInfo";

    vector<string> * names = new  vector<string>;

    MysqlResult res;
	try
	{
		res.dataSet = pConn->executeQuery(sql);

		if (res.dataSet)
		{
            while (res.dataSet->next())
            {
                string family = res.dataSet->getString("FamilyName");
                names->push_back( family );
            }
		}
	}
	catch(sql::SQLException & e)
    {
        cout << __LINE__ << "\n";
        cout << "# ERR: " << e.what() << "\n";
    }

    // 拷贝数据
    _WORKER->Lock();

    for (size_t i=0;  i < names->size(); i++)
    {
        _WORKER->AddFamilyName(  (*names)[i].c_str(), index );
    }

    _WORKER->Unlock();

    //
    delete names;
    g_SelectCounter.DecRef();
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void  Async_RenamePlayerNameManage(CMsgTyped* pmsg,  sql::Statement* pConn)
{
    if (!pmsg || !pConn) return;

    const char* oldname = pmsg->StringVal();
    const char* newname = pmsg->StringVal();

    printf("p->>>>> %s %s\n", oldname, newname);

    char sql[256];
    sprintf(sql, "update GlobalRoles set name = '%s' where name = '%s'",  newname, oldname );

    try
	{
		pConn->executeUpdate(sql);
	}
	catch(sql::SQLException & e)
    {
    }

    g_UpdateCounter.DecRef();
}

void  Async_RenamePlayerNameRole(CMsgTyped* pmsg,  sql::Statement* pConn)
{
  if (!pmsg || !pConn) return;

    const char* oldname = pmsg->StringVal();
    const char* newname = pmsg->StringVal();

    printf("p->>>>> %s %s\n", oldname, newname);

    // 更新
    char sql[256];
    try
	{
	    sprintf(sql, "update GameRoles set name = '%s' where name = '%s'",  newname, oldname );
		pConn->executeUpdate(sql);
	}
	catch(sql::SQLException & e){ }

    // 更新
    try
	{
	    sprintf(sql, "update GameRoles_bk set name = '%s' where name = '%s'",  newname, oldname );
		pConn->executeUpdate(sql);
	}
	catch(sql::SQLException & e){ }

	// 更新
    try
	{
	    sprintf(sql, "update GameRoles_Detail set name = '%s' where name = '%s'",  newname, oldname );
		pConn->executeUpdate(sql);
	}
	catch(sql::SQLException & e){ }

    g_UpdateCounter.DecRef();
}

void  Async_RenamePlayerNameMas(CMsgTyped* pmsg,  sql::Statement* pConn)
{
  if (!pmsg || !pConn) return;

    const char* oldname = pmsg->StringVal();
    const char* newname = pmsg->StringVal();

    printf("p->>>>> %s %s\n", oldname, newname);

    char sql[256];
    try
	{
	    //update MasterInfo  set MasterName = '222' where MasterName = '123'
	    sprintf(sql, "update MasterInfo  set MasterName = '%s' where MasterName = '%s'",  newname, oldname );
		pConn->executeUpdate(sql);
	}
	catch(sql::SQLException & e)
    {
    }

    //StudentInfo
    //StudentName VARCHAR(64) NOT NULL,
	//MasterName VARCHAR(64) NOT NULL,
    try
	{
	    //update StudentInfo  set StudentName = '222' where StudentName = '123'
	    sprintf(sql, "update StudentInfo  set StudentName = '%s' where StudentName = '%s'",  newname, oldname );
		pConn->executeUpdate(sql);
	}
	catch(sql::SQLException & e)
    {
    }

    try
	{
	    //update StudentInfo  set MasterName = '222' where MasterName = '123'
	    sprintf(sql, "update StudentInfo  set MasterName = '%s' where MasterName = '%s'",  newname, oldname );
		pConn->executeUpdate(sql);
	}
	catch(sql::SQLException & e)
    {
    }

    g_UpdateCounter.DecRef();
}

void  Async_RenamePlayerNameFamily(CMsgTyped* pmsg,  sql::Statement* pConn)
{
  if (!pmsg || !pConn) return;

    const char* oldname = pmsg->StringVal();
    const char* newname = pmsg->StringVal();

    printf("p->>>>> %s %s\n", oldname, newname);

    char sql[256];
   // update FamilyMember set MemberName = '1' where MemberName = '1';
    sprintf(sql, "update FamilyMember set MemberName = '%s' where MemberName = '%s';",  newname, oldname );

    try
	{
		pConn->executeUpdate(sql);
	}
	catch(sql::SQLException & e)
    {
    }

    g_UpdateCounter.DecRef();
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void  Async_RenameFamilyName(CMsgTyped* pmsg,  sql::Statement* pConn)
{
    if (!pmsg || !pConn) return;

    const char* oldname = pmsg->StringVal();
    const char* newname = pmsg->StringVal();

    printf("F->>>>> %s %s\n", oldname, newname);

    char sql[256];
    try
	{
	    sprintf(sql, "update FamilyInfo set FamilyName = '%s' where FamilyName = '%s'",  newname,  oldname );
		pConn->executeUpdate(sql);
	}
	catch(sql::SQLException & e)
    {
    }

    try
	{
	    sprintf(sql, " update FamilyMember set FamilyName = '%s' where FamilyName = '%s'",  newname,  oldname );
		pConn->executeUpdate(sql);
	}
	catch(sql::SQLException & e)
    {
    }

    g_UpdateCounter.DecRef();
}

void  Async_RenamePlayerNameMail(CMsgTyped* pmsg,  sql::Statement* pConn)
{
    if (!pmsg || !pConn) return;

    const char* oldname = pmsg->StringVal();
    const char* newname = pmsg->StringVal();

    printf("F->>>>> %s %s\n", oldname, newname);

    char sql[256];
    try
	{
	    sprintf(sql, "update MailTable set Owner = '%s' where Owner = '%s'",  newname,  oldname );
		pConn->executeUpdate(sql);
	}
	catch(sql::SQLException & e)
    {
    }

    try
	{
	    sprintf(sql, " update MailTable set Sender = '%s' where Sender = '%s'",  newname,  oldname );
		pConn->executeUpdate(sql);
	}
	catch(sql::SQLException & e)
    {
    }

    try
	{
	    sprintf(sql, " update MailTable set Receiver = '%s' where Receiver = '%s'",  newname,  oldname );
		pConn->executeUpdate(sql);
	}
	catch(sql::SQLException & e)
    {
    }

    g_UpdateCounter.DecRef();
}

void  Async_ReIDManage(CMsgTyped* pmsg,  sql::Statement* pConn)
{
    if (!pmsg || !pConn) return;

    int id_add = pmsg->IntVal();

    // update GlobalRoles set RoleId = RoleId + 1
    char sql[256];

    sprintf(sql, "update GlobalRoles set RoleId = RoleId + %d", id_add);

    try
	{
		pConn->executeUpdate(sql);
	}
	catch(sql::SQLException & e)
    {

    }

    g_UpRoleIDCounter.DecRef();
}

void  Async_ReIDRole(CMsgTyped* pmsg,  sql::Statement* pConn)
{
     if (!pmsg || !pConn) return;

    int id_add = pmsg->IntVal();

    // update GlobalRoles set RoleId = RoleId + 1
    char sql[256];

    try
	{
	    sprintf(sql, "update GameRoles set RoleId = RoleId + %d", id_add);
		pConn->executeUpdate(sql);
	}
	catch(sql::SQLException & e){}

    try
	{
	    sprintf(sql, "update GameRoles_bk set RoleId = RoleId + %d", id_add);
		pConn->executeUpdate(sql);
	}
	catch(sql::SQLException & e){}

    try
	{
	    sprintf(sql, "update GameRoles_Detail set RoleId = RoleId + %d", id_add);
		pConn->executeUpdate(sql);
	}
	catch(sql::SQLException & e){}

    g_UpRoleIDCounter.DecRef();
}




